package com.chenglai.seckill.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.seckill.pojo.SeckillInfo;
import com.chenglai.seckill.service.SeckillInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/23 14:22
 */
@RestController
@RequestMapping("/seckillInfo")
public class SeckillInfoController {

    @Resource
    private SeckillInfoService seckillInfoService;

    //秒杀活动开始前预热
    @PostMapping("/getSeckillAllList")
    @ApiOperation(value = "获取门店所有的秒杀活动信息")
    public Result getSeckillAllList(@ApiParam(name = "page", value = "页数") @RequestParam("page") Integer page,
                                    @ApiParam(name = "size", value = "size") @RequestParam("size") Integer size,
                                    @ApiParam(name = "shopId", value = "门店Id") @RequestParam("shopId") Integer shopId) {
        try {
            PageInfo pageInfo = seckillInfoService.getSeckillAllList(page, size, shopId);
            return new Result(true, StatusCode.OK, "操作成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/getLatelySeckillInfo")
    @ApiOperation(value = "获取下一个活动信息")
    public Result getLatelySeckillInfo(@ApiParam(name = "shopId", value = "门店Id") @RequestParam("shopId") Integer shopId) {
        try {
            SeckillInfo seckillInfo = seckillInfoService.getLatelySeckillInfo(shopId);
            return new Result(true, StatusCode.OK, "操作成功", seckillInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }


    @PostMapping("/editSeckillInfp")
    @ApiOperation(value = "添加/修改秒杀活动")
    public Result editArticleCategory(SeckillInfo seckillInfo) {
        try {
            seckillInfoService.editSeckillInfo(seckillInfo);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/detail")
    @ApiOperation(value = "秒杀活动详情")
    public Result categoryDetail(Integer seckillId) {
        try {
            SeckillInfo detail = seckillInfoService.detail(seckillId);
            return new Result(true, StatusCode.OK, "查询成功", detail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }





}
